/*
 * This calendar application is brought to you
 * by Group 3 of User Interface Programming II, vt11.
 *
 * Group Members:
 * Pooya | Flora | Monica | Can
 */
package vical.model;

import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
 * This class deals with internationalization of this calendar application.
 * call the loadLanguage first, then use the getString(..) or getMnemonic(..)
 *
 * @author MONICA KUMAR
 * @author Can
 */
public class Internationalization {

    private final static String baseName = "vical.resources/language";
    private static ResourceBundle currentResource = null;

    private Internationalization() {
    }

    public static int loadLanguage(Locale newLocale) {
        try {
            currentResource = ResourceBundle.getBundle(
                    baseName + "_" + newLocale.toString());
        } catch (MissingResourceException mre) {
            System.out.println(mre.getMessage());
            return -1;
        }
        return 0;
    }

    /**
     * get certain String from resourceBundle, return "NULL" if error
     */
    public static String getString(String key) {
        String keyValue;
        try {
            keyValue = currentResource.getString(key);
        } catch (MissingResourceException mre) {
            System.out.println(mre.getMessage());
            return "NULL";
        } catch (NullPointerException npe) {
            System.out.println(npe.getMessage());
            return "NULL";
        } catch (ClassCastException cce) {
            System.out.println(cce.getMessage());
            return "NULL";
        }
        return keyValue;
    }

    // return -1 if error
    public static int getMnemonic(String key) {
        String mnemonicString = Internationalization.getString(key);
        if (mnemonicString.equalsIgnoreCase("NULL")) {
            return -1;
        } else {
            return (int) (mnemonicString.charAt(0));
        }
    }

    // for test
    public static void main(String[] args) {
        Locale locale = new Locale("en");
        System.out.println(locale.toString());
        Internationalization.loadLanguage(locale);
        System.out.println(Internationalization.getString("fileMenu"));
    }
}
